﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    public class AccountRepresentativeDataAccess : BaseDataAccess<Business.Entities.AccountRepresentative>
    {
        public override GMAC.ReportingSystem.Business.Entities.AccountRepresentative Get(object id)
        {
            ArrayList parameters = new ArrayList() { id };
            DataTable dt = DataAccessComponent.ExecuteSelect("procAccountRepresentativeByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        public AccountRepresentativeCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procAccountRepresentativeSelect", null);
            return ReadTable(dt) as AccountRepresentativeCollection;
        }

        protected override GMAC.ReportingSystem.Business.Entities.AccountRepresentative ReadRow(System.Data.DataRow row)
        {
            AccountRepresentative AccountRepresentative = null;
            if (row != null)
            {
                try
                {
                    AccountRepresentative = new AccountRepresentative();
                    AccountRepresentative.ID = (int)row["ID"];
                    AccountRepresentative.FistName = row["FistName"].ToString();
                    AccountRepresentative.LastName = row["FistName"].ToString() +" "+ row["LastName"].ToString();
                    AccountRepresentative.Name = row["LastName"].ToString();
                    AccountRepresentative.Email = row["Email"].ToString();
                    AccountRepresentative.IdentificationNumber = row["IdentificationNumber"].ToString();
                    AccountRepresentative.Phone = row["Phone"].ToString();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return AccountRepresentative;
        }

        protected override List<GMAC.ReportingSystem.Business.Entities.AccountRepresentative> ReadTable(System.Data.DataTable table)
        {
            try
            {
                AccountRepresentativeCollection AccountRepresentatives = new AccountRepresentativeCollection();
                AccountRepresentative AccountRepresentative;
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        AccountRepresentative = ReadRow(row);
                        if (AccountRepresentative != null)
                        {
                            AccountRepresentatives.Add(AccountRepresentative);
                        }
                    }
                }
                return AccountRepresentatives;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public AccountRepresentativeCollection Search(string FirstName, string LastName, string IdentificationNumber, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { FirstName, LastName, IdentificationNumber, paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procAccountRepresentativeByFilterSelect", parameters);
                AccountRepresentativeCollection result = null;
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as AccountRepresentativeCollection;
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }

        }


    }
}
